package com.yonyougov.cruxtestcase.component.uc;

import com.yonyougov.cruxtestcase.component.CommonLogin;
import com.yonyougov.cruxtestcase.component.ZWCDInvoke;
import com.yonyougov.cruxtestcase.component.uc.dto.UserDto;
import com.yonyougov.cruxtestcase.component.uc.dto.UserOfficeDto;
import com.yonyougov.cruxtestcase.dto.BaseResponseDto;
import com.yonyougov.cruxtestcase.dto.BaseTestReqDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户相关的API:用户登陆,登出
 */
@Component
public class UcClient {
    private final ZWCDInvoke zwcdInvoke;
    private final CommonLogin commonLogin;
    private final RestTemplate restTemplate;
    public UcClient(ZWCDInvoke zwcdInvoke, CommonLogin commonLogin, RestTemplate restTemplate) {
        this.zwcdInvoke = zwcdInvoke;
        this.commonLogin = commonLogin;
        this.restTemplate = restTemplate;
    }
    /**
     * 用户登陆
     * @param baseUrl  基础路径
     * @param username 用户名称
     * @param password 用户密码
     * @return BaseResponseDto [token 凭证]
     */
    public BaseResponseDto login(String baseUrl, String username, String password){
        BaseResponseDto baseResponseDto = new BaseResponseDto();
//        String baseUrl = zwcdInvoke.getBaseUrl(envName);
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, username, password);
            if (StringUtils.isNotBlank(token)) {
                baseResponseDto.setSuccess(true);
                baseResponseDto.setData("token",token);
            } else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        } else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    /**
     * 用户登出
     * @param baseUrl 基础路径
     * @param token  凭证
     * @return boolean
     */
    public boolean logout(String baseUrl,String token){
        HttpHeaders headers=new HttpHeaders();
        headers.set("Authorization","Bearer " + token);
//        String baseUrl = zwcdInvoke.getBaseUrl(envName);
        String url = baseUrl + "/crux-auth/logout" + "?redirect_uri=" + "/";
        HttpEntity<String> httpEntity = new HttpEntity<>(null, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            return true;
        }
        return false;
    }

    /**
     *
     * @param baseUrl   基本路径
     * @param token   凭证
     * @param userDto 要保存的用户实体
     * @return UserDto 用户
     */
    public UserDto saveUser(String baseUrl,String token, UserDto userDto){
        HttpHeaders headers=new HttpHeaders();
        headers.set("Authorization","Bearer " + token);
        HttpEntity<UserDto> httpEntity = new HttpEntity<>(userDto, headers);
        ResponseEntity<UserDto> response = restTemplate.exchange(baseUrl+"/crux-uc/api/user?fetchProperties=id", HttpMethod.POST, httpEntity, UserDto.class);
        if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
            userDto = response.getBody();
        }
        return userDto;
    }
    /**
     * 删除用户任职
     * @param baseUrl 基本路径
     * @param token 凭证
     * @param userOfficeDto 参数
     * @return UserOfficeDto
     */
    public UserOfficeDto delUserOffice(String baseUrl,String token, UserOfficeDto userOfficeDto){
        HttpHeaders headers=new HttpHeaders();
        headers.set("Authorization","Bearer " + token);
        HttpEntity<UserOfficeDto> httpEntity = new HttpEntity<>(userOfficeDto, headers);
        ResponseEntity<UserOfficeDto> response = restTemplate.exchange(baseUrl+"/crux-uc/api/useroffice/"+userOfficeDto.getId(), HttpMethod.DELETE, httpEntity, UserOfficeDto.class);
        if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
            userOfficeDto = response.getBody();
        }
        return userOfficeDto;
    }

    /**
     * 通过用户及组织查询用户任职
     * @param baseUrl 基本路径
     * @param token 凭证
     * @param userDto 参数
     * @return UserOfficeDto
     */
    public UserOfficeDto queryUserOfficeByUserAndOrg(String baseUrl, String token, UserDto userDto){
        HttpHeaders headers=new HttpHeaders();
        UserOfficeDto userOfficeDto = null;
        headers.set("Authorization","Bearer " + token);
        HttpEntity<UserDto> httpEntity = new HttpEntity<>(userDto, headers);
        //ResponseEntity<List> response = restTemplate.exchange(baseUrl+"/crux-uc/api/useroffice?user.id="+userDto.getId()+"&organization="+userDto.getOrganization(), HttpMethod.GET, httpEntity, List.class);
        ResponseEntity<List<UserOfficeDto>> response = restTemplate.exchange(baseUrl+"/crux-uc/api/useroffice?user.id="+userDto.getId()+"&organization="+userDto.getOrganization(), HttpMethod.GET, httpEntity, new ParameterizedTypeReference<List<UserOfficeDto>>() {});
        if (response.getStatusCode() == HttpStatus.OK && response.hasBody() && response.getBody().size()>0) {
            userOfficeDto = response.getBody().get(0);
        }
        return userOfficeDto;
    }
}
